﻿using System;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCCourseChapter : System.Web.UI.UserControl
	{
		private ICourseProvider _CourseProvider;

		public int CourseChapterID
		{
			get
			{
				return CommonHelper.QueryStringInt("coursechapterid");
			}
		}

		public CourseChapter TheCourseChapter
		{
			get;
			set;
		}

		public string CourseChapterVideoAddress
		{
			get;
			set;
		}

		public string VideoAddress
		{
			get;
			set;
		}

		public string ServerURL
		{
			get
			{
				if (Request.Url.Port > 0)
				{
					return string.Format("http://{0}:{1}", Request.Url.Host, Request.Url.Port.ToString());
				}
				else
				{
					return "http://" + Request.Url.Host;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindData();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			UpdateRLT_UserAndCourse();
		}

		protected void rptCourseChapters_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null)
			{
				var dataItem = e.Item.DataItem as CourseChapter;
				var hlCourseChapter = e.Item.FindControl("hlCourseChapter") as HyperLink;

				hlCourseChapter.Text = dataItem.CourseChapterName;
				hlCourseChapter.NavigateUrl = "~/CourseChapterDetail.aspx?coursechapterid=" + dataItem.CourseChapterID.ToString();

				if (dataItem.CourseChapterID == this.CourseChapterID)
				{
					hlCourseChapter.Enabled = false;
				}
			}
		}

		private void BindCourseChapter()
		{
			if (CourseChapterID > 0)
			{
				if (_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				TheCourseChapter = _CourseProvider.GetCourseChapter(this.CourseChapterID);

				if (TheCourseChapter != null && TheCourseChapter.CourseID > 0)
				{
					rptCourseChapters.DataSource = _CourseProvider.GetCourseChapterByCourse(TheCourseChapter.CourseID.Value);
					rptCourseChapters.DataBind();
				}
			}
		}

		private void BindData()
		{
			if (CourseChapterID > 0)
			{
				if (_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				BindCourseChapter();

				if (TheCourseChapter == null)
				{
					TheCourseChapter = _CourseProvider.GetCourseChapter(this.CourseChapterID);
				}

				if (TheCourseChapter != null)
				{
					this.VideoAddress = TheCourseChapter.CourseChapterVideoAddress;

					//if (System.IO.Path.GetExtension(VideoAddress).Equals(".flv"))
					//{
					//  this.VideoAddress = ServerURL + "/template/FLVPlayer.swf?video=" + VideoAddress;
					//}
				}
			}
		}

		private void UpdateRLT_UserAndCourse()
		{
			var user = SiteContext.Current.CurrentUser;

			if (TheCourseChapter != null && user != null)
			{
				var rltUserAndCourse = new RLT_USERANDCOURCE() { CourseID = TheCourseChapter.CourseID.GetValueOrDefault(0), UserID = user.UserID, LastOperateTime = DateTime.Now };

				if (_CourseProvider == null)
				{
					_CourseProvider = new CourseProvider();
				}

				_CourseProvider.UpdateRLT_UserAndCourse(rltUserAndCourse);
			}
		}
	}
}